# -*- coding: utf-8 -*-
# @Time    : 2020/9/14 18:30
# @Author  : Mr. Cui
# @File    : send_msg.py
# @Software: PyCharm
import smtplib
import email.mime.text
import email.mime.multipart
import json

import requests


def ding_send(token, content):
    """

    :param token: 钉钉群token
    :param content:
    :return:
    """
    HEADERS = {"Content-Type": "application/json ;charset=utf-8 "}
    data = {"msgtype": "text", "text": {"content": content}, "at": {"atMobiles": [], "isAtAll": "false"}}
    data = json.dumps(data)
    res = requests.post(token, data=data, headers=HEADERS)
    return res.text


def email_send(receiver, subject, content):
    """

    :param receiver: 收件人邮箱
    :param subject: 邮件标题
    :param content: 邮件内容
    :return:
    """
    email_name = 'mantis@360che.com'
    email_passwd ='360CHEche1'

    msg = email.mime.multipart.MIMEMultipart()
    msg['from'] = email_name
    msg['to'] = str(receiver)[1:-1].replace("'", "")
    msg['subject'] = subject
    txt = email.mime.text.MIMEText(content)
    msg.attach(txt)
    smtp = smtplib.SMTP()
    smtp.connect('smtp.mxhichina.com', '25')
    smtp.login(email_name, email_passwd)
    smtp.sendmail(email_name, receiver, str(msg))
    smtp.quit()

def email_send_html(receiver, subject, content):
    """

    :param receiver: 收件人邮箱
    :param subject: 邮件标题
    :param content: 邮件内容
    :return:
    """
    email_name = 'mantis@360che.com'
    email_passwd ='360CHEche1'

    msg = email.mime.multipart.MIMEMultipart()
    msg['from'] = email_name
    msg['to'] = str(receiver)[1:-1].replace("'", "")
    msg['subject'] = subject
    txt = email.mime.text.MIMEText(content,'html', 'utf-8')
    msg.attach(txt)
    smtp = smtplib.SMTP()
    smtp.connect('smtp.mxhichina.com', '25')
    smtp.login(email_name, email_passwd)
    smtp.sendmail(email_name, receiver, str(msg))
    smtp.quit()